home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 October: System Software / Dev.CD Oct 94.toast / What's New? / Sample Code / StandardFileIcons / SFIconsInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  3.8 KB  |  146 lines  |  [TEXT/MPS ]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    StandardFileIcons: A sample control panel changing behavior of StandardFile.
  5. **
  6. **    by Brian Bechtel, Apple Developer Technical Support
  7. **
  8. **    File:        SFIconsInit.c
  9. **
  10. **    Copyright © 1994 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DTS Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. **
  21. ** Change history (most recent first)
  22. **
  23. **    <1>    940811    BL°B    Check for System 7.  Display failure icon and do
  24. **                        nothing else if earlier than System 7.  Macintosh
  25. **                        Easy Open is the first product which actually
  26. **                        provided the desktop icon feature; it can work all
  27. **                        the way back to System 7, potentially.
  28. **    <0>    940802    BL°B    First implementation
  29. */
  30.  
  31. /*
  32. ** Check for the existence of our SFIcons preference file.  If the file exists,
  33. ** then we want to use generic icons instead of the color icons Standard File Package
  34. ** uses.  This all works only if you have System Update 3.0 installed, or
  35. ** System 7.5 or later (which incorporate the new Standard File Package introduced
  36. ** in System Update 3.0)
  37. **/
  38.  
  39. #include    <Files.h>
  40. #include    <Folders.h>
  41. #include    <Memory.h>
  42. #include    <GestaltEqu.h>
  43. #include    <ToolUtils.h>
  44.  
  45. #include    "GestaltValue.h"
  46.  
  47. enum {
  48.     gestaltStandardFileUseGenericIcons = 3,
  49.     kSFIconsPrefFileName = 128,
  50.     kOurCustomIcon = 128,
  51.     kOurGenericIcon = 129,
  52.     kFailedIcon = 130
  53. };
  54.  
  55.  
  56. /* From ShowIcon7.c */
  57. pascal void ShowIcon7(short iconId, Boolean advance);
  58.  
  59. void     main(void);
  60. Boolean UseGenericIcons(Handle);
  61. void    Generic(void);
  62. Boolean    OKSystem(void);
  63.  
  64. /*
  65. **    Get the name of our preference file from a 'STR ' resource
  66. **  in our control panel.  Check if that preference file exists.
  67. **  If that preference file exists, we should set the bit in the
  68. **  appropriate Gestalt selector which tells the standard file 
  69. **  package whether to use generic icons from within the PACK 
  70. **  (faster) or grab icons from the desktop file (slower).
  71. **/
  72. void main(void)
  73. {
  74.     Handle    prefFileName;
  75.  
  76.     if (!OKSystem())
  77.     {
  78.         ShowIcon7(kFailedIcon, true);
  79.         return;
  80.     }
  81.     prefFileName = (Handle)GetString(kSFIconsPrefFileName);
  82.     if (prefFileName != nil)
  83.     {
  84.         HLock(prefFileName);
  85.         if (UseGenericIcons(prefFileName))
  86.         {
  87.             Generic();
  88.             ShowIcon7(kOurGenericIcon, true);
  89.         }
  90.         else
  91.             ShowIcon7(kOurCustomIcon, true);
  92.         HUnlock(prefFileName);
  93.         DisposeHandle(prefFileName);
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. /*
  100. ** Decide to use generic icons or not. See if the preference file exists.  If it 
  101. ** exists, use generic icons.
  102. **/
  103. Boolean UseGenericIcons(Handle preferenceFileName)
  104. {
  105.     OSErr    err;
  106.     FSSpec    spec;
  107.     short    foundVRefNum;
  108.     long    foundDirID;
  109.     
  110.     err = FindFolder(kOnSystemDisk,kPreferencesFolderType,false,&foundVRefNum,&foundDirID);
  111.     if (!err)
  112.         err = FSMakeFSSpec(foundVRefNum,foundDirID,(StringPtr)*preferenceFileName,&spec);
  113.     return (err == noErr);
  114. }
  115.  
  116. /*
  117. ** Generic
  118. ** 
  119. ** Set the Gestalt bit in gestaltStandardFileAttr which says to use Generic Icons
  120. **/
  121. void Generic(void)
  122. {
  123.     long    gestaltValue;
  124.     OSErr    err;
  125.     
  126.     err = Gestalt(gestaltStandardFileAttr, &gestaltValue);
  127.     if (!err)
  128.     {
  129.         gestaltValue |= (1 << gestaltStandardFileUseGenericIcons);
  130.         ReplaceGestaltValue(gestaltStandardFileAttr, gestaltValue);
  131.     }
  132. }
  133.  
  134.  
  135. /* Check System Version and return false if primary digit is < 7 (e.g. System 6)*/
  136. Boolean OKSystem(void)
  137. {
  138.     long    response;
  139.     OSErr    err;
  140.     
  141.     err = Gestalt(gestaltSystemVersion, &response);
  142.     
  143.     return ( (err == noErr) && ((response >> 8) >= 0x07) );
  144. }
  145.     
  146.